
import java.awt.HeadlessException;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import javax.swing.JOptionPane;

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/**
 *
 * @author TQNINH
 */
public class EmployeeRecords extends javax.swing.JFrame {
    private Connection cnEmployee = null;
    private final String url = "jdbc:sqlserver://";
    private final String serverName = "localhost";
    private final String portName = "1433";
    private final String instanceName = "SQL11";
    private final String databaseName = "DeveloperApps";
    private final String userName = "sa";
    private final String password = "";
    private Statement stmt;
    private ResultSet rs;
    /**
     * Creates new form EmployeeRecords
     */
    public EmployeeRecords() {
        try {
            Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");
            cnEmployee = DriverManager.getConnection(getConnectionUrl(), userName, password);
            if(cnEmployee != null) {
                JOptionPane.showMessageDialog(null, "Connection is successful!!");
                String sqlStmt = "Select * from Employee";
                stmt = cnEmployee.createStatement();
                rs = stmt.executeQuery(sqlStmt);
            }
        } catch(SQLException ex) {
            if(cnEmployee == null) {
                JOptionPane.showMessageDialog(null, "Connection is not successful!");
            }
            ex.printStackTrace();
        } catch(ClassNotFoundException ex) {
            JOptionPane.showMessageDialog(null, "Can't not find class");
            JOptionPane.showMessageDialog(null, "Connection is not successful!");
            System.exit(0);
            ex.printStackTrace();
        }
        initComponents();
    }
    
    private String getConnectionUrl() {
        return url + serverName + ":" + portName + ";instanceName=" + instanceName + ";databaseName=" + databaseName;
    }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        pnlEmployeeDetail = new javax.swing.JPanel();
        lblCode = new javax.swing.JLabel();
        lblName = new javax.swing.JLabel();
        lblSalary = new javax.swing.JLabel();
        txtName = new javax.swing.JTextField();
        txtCode = new javax.swing.JTextField();
        txtSalary = new javax.swing.JTextField();
        lblEmployeeDetails = new javax.swing.JLabel();
        btnNext = new javax.swing.JButton();
        btnExit = new javax.swing.JButton();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
        setTitle("Employee Records");
        setMinimumSize(new java.awt.Dimension(390, 280));
        setResizable(false);
        getContentPane().setLayout(null);

        pnlEmployeeDetail.setBorder(javax.swing.BorderFactory.createTitledBorder(null, "Employee Detail", javax.swing.border.TitledBorder.DEFAULT_JUSTIFICATION, javax.swing.border.TitledBorder.DEFAULT_POSITION, new java.awt.Font("Tahoma", 1, 11))); // NOI18N

        lblCode.setText("Code:");

        lblName.setText("Name:");

        lblSalary.setText("Salary:");

        javax.swing.GroupLayout pnlEmployeeDetailLayout = new javax.swing.GroupLayout(pnlEmployeeDetail);
        pnlEmployeeDetail.setLayout(pnlEmployeeDetailLayout);
        pnlEmployeeDetailLayout.setHorizontalGroup(
            pnlEmployeeDetailLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(pnlEmployeeDetailLayout.createSequentialGroup()
                .addGap(24, 24, 24)
                .addGroup(pnlEmployeeDetailLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                    .addGroup(pnlEmployeeDetailLayout.createSequentialGroup()
                        .addGroup(pnlEmployeeDetailLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, pnlEmployeeDetailLayout.createSequentialGroup()
                                .addComponent(lblName)
                                .addGap(18, 18, 18))
                            .addGroup(pnlEmployeeDetailLayout.createSequentialGroup()
                                .addComponent(lblCode)
                                .addGap(20, 20, 20)))
                        .addGap(3, 3, 3)
                        .addGroup(pnlEmployeeDetailLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(txtName, javax.swing.GroupLayout.PREFERRED_SIZE, 267, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(txtCode, javax.swing.GroupLayout.PREFERRED_SIZE, 267, javax.swing.GroupLayout.PREFERRED_SIZE)))
                    .addGroup(pnlEmployeeDetailLayout.createSequentialGroup()
                        .addComponent(lblSalary)
                        .addGap(18, 18, 18)
                        .addComponent(txtSalary)))
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );
        pnlEmployeeDetailLayout.setVerticalGroup(
            pnlEmployeeDetailLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(pnlEmployeeDetailLayout.createSequentialGroup()
                .addGap(4, 4, 4)
                .addGroup(pnlEmployeeDetailLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(txtName, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(lblName))
                .addGap(10, 10, 10)
                .addGroup(pnlEmployeeDetailLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(txtCode, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(lblCode))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(pnlEmployeeDetailLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(lblSalary)
                    .addComponent(txtSalary, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addContainerGap())
        );

        getContentPane().add(pnlEmployeeDetail);
        pnlEmployeeDetail.setBounds(10, 70, 365, 120);

        lblEmployeeDetails.setFont(new java.awt.Font("Tahoma", 1, 18)); // NOI18N
        lblEmployeeDetails.setForeground(new java.awt.Color(255, 0, 51));
        lblEmployeeDetails.setHorizontalAlignment(javax.swing.SwingConstants.CENTER);
        lblEmployeeDetails.setText("Employee Details");
        getContentPane().add(lblEmployeeDetails);
        lblEmployeeDetails.setBounds(0, 10, 390, 50);

        btnNext.setText("Next");
        btnNext.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnNextActionPerformed(evt);
            }
        });
        getContentPane().add(btnNext);
        btnNext.setBounds(80, 200, 80, 25);

        btnExit.setText("Exit");
        btnExit.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnExitActionPerformed(evt);
            }
        });
        getContentPane().add(btnExit);
        btnExit.setBounds(230, 200, 80, 25);

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void btnExitActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnExitActionPerformed
        // TODO add your handling code here:
        System.exit(0);
        try {
            rs.close();
            stmt.close();
            cnEmployee.close();
        } catch(SQLException ex) {
            ex.printStackTrace();
        }
    }//GEN-LAST:event_btnExitActionPerformed

    private void btnNextActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnNextActionPerformed
        try {
            if(!rs.next()){
                btnNext.setEnabled(false);
                JOptionPane.showMessageDialog(null, "You have reached the end of the ResultSet.");
            } else {
                txtName.setText(rs.getString(1));
                txtCode.setText(rs.getString(2));
                txtSalary.setText(Float.valueOf(rs.getFloat(3)).toString());
            }
        } catch(HeadlessException ex) {
            ex.printStackTrace();
        } catch(SQLException ex) {
            ex.printStackTrace();
        }
    }//GEN-LAST:event_btnNextActionPerformed

    /**
     * @param args the command line arguments
     */
    public static void main(String args[]) {
        /* Set the Nimbus look and feel */
        //<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
        /* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel.
         * For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html 
         */
        try {
            for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
                if ("Windows".equals(info.getName())) {
                    javax.swing.UIManager.setLookAndFeel(info.getClassName());
                    break;
                }
            }
        } catch (ClassNotFoundException | InstantiationException | IllegalAccessException | javax.swing.UnsupportedLookAndFeelException ex) {
            java.util.logging.Logger.getLogger(EmployeeRecords.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        }
        //</editor-fold>

        /* Create and display the form */
        java.awt.EventQueue.invokeLater(new Runnable() {
            @Override
            public void run() {
                new EmployeeRecords().setVisible(true);
            }
        });
    }
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton btnExit;
    private javax.swing.JButton btnNext;
    private javax.swing.JLabel lblCode;
    private javax.swing.JLabel lblEmployeeDetails;
    private javax.swing.JLabel lblName;
    private javax.swing.JLabel lblSalary;
    private javax.swing.JPanel pnlEmployeeDetail;
    private javax.swing.JTextField txtCode;
    private javax.swing.JTextField txtName;
    private javax.swing.JTextField txtSalary;
    // End of variables declaration//GEN-END:variables
}
